package cj.web.portal.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.Page;
import cj.web.admin.constants.CategoryEnum;
import cj.web.admin.constants.MatrixTypeEnum;
import cj.web.admin.constants.NewsCategoryEnum;
import cj.web.portal.dao.LineDAO;
import cj.web.portal.dao.NewMediaDAO;

/**
 * 新媒体矩阵、微博汇、微信汇
 * 
 * @author HanMin
 *
 *         2015年12月30日
 */
@Path("newmedia")
public class NewMediaController {

	private static Logger logger = Logger.getLogger(NewMediaController.class.getName());

	@Autowired
	private LineDAO lineDAO;
	@Autowired
	private NewMediaDAO newMediaDAO;
	
	// 新兴媒体
	@Get("xxmt")
	public String showXXMT(Model model) {
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));// 集团品牌活动列表
		return "newmedia/xxmt.vm";
	}

	// 网站
	@Get("wz")
	public String showWZ(Model model) {
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));// 集团品牌活动列表
		return "newmedia/wz.vm";
	}
	
	// 数字报
	@Get("szb")
	public String showSZB(Model model) {
		
		List<Map> szblis = new ArrayList<Map>();
		
		String cjrb = "http://cjrb.cjn.cn/";
		String whwb = "http://whwb.cjn.cn/";
		String whcb = "http://whcb.cjn.cn/";
		String cjweek ="http://cjweek.cjn.cn/";
		String tzsb ="http://tzsb.cjn.cn/";
		String rwhb ="http://rwhb.cjn.cn/";
		String xsh ="http://xsh.cjn.cn/";
			
		String ym = DateUtils.getNowTime("yyyy-MM");
		String d = DateUtils.getNowTime("dd");
		String ymd = DateUtils.getNowTime(DateUtils.DATE_SMALL_STR2);
		String img = "images/"+ym+"/"+d+"/1/"+ymd+"1_brief.jpg";
		
		//cjrb
		Map repcjrb = new HashMap();
		repcjrb.put("webste", cjrb);
		repcjrb.put("img", cjrb+img);
		//whwb
		Map repwhwb = new HashMap();
		repwhwb.put("webste", whwb);
		repwhwb.put("img", whwb+img);
		//whcb
		Map repwhcb = new HashMap();
		repwhcb.put("webste", whcb);
		repwhcb.put("img", whcb+img);
		//cjweek
		Map repcjzb = new HashMap();
		repcjzb.put("webste", cjweek);
		repcjzb.put("img", cjweek+"images/2015-12/31/1/201512311_brief.jpg");
		//tzsb
		Map reptzsb = new HashMap();
		reptzsb.put("webste", tzsb);
		reptzsb.put("img", tzsb+"images/2015-05/16/1/201505161_brief.jpg");
		//rwhb
		Map reprwhb = new HashMap();
		reprwhb.put("webste", rwhb);
		reprwhb.put("img", rwhb+"images/2013-04-25/1/78311366860008995.jpg");
		//xsh
		Map repxsh = new HashMap();
		repxsh.put("webste", xsh);
		repxsh.put("img", xsh+"images/2014-02/22/1/201402221_brief.jpg");
		
		szblis.add(repcjrb);
		szblis.add(repwhwb);
		szblis.add(repwhcb);
		szblis.add(repcjzb);
		szblis.add(reptzsb);
		szblis.add(reprwhb);
		szblis.add(repxsh);
		
		model.add("szblis", szblis);// 集团品牌活动列表
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));// 集团品牌活动列表
		return "newmedia/szb.vm";
	}
	
	//app
	@Get("app")
	public String showAPP(Model model) {
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));// 集团品牌活动列表
		return "newmedia/app.vm";
	}

	// 微博汇
	@Get("wbh")
	public String showWBH(Model model, @Param("start") int start, @Param("length") int length) {
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));// 集团品牌活动列表
		return "newmedia/wbh.vm";
	}

	// 微信汇
	@Get("wxh")
	public String showWXH(Model model, @Param("start") int start, @Param("length") int length) {
		// 类型微信列表
		List<Map> gfwx =  newMediaDAO.queryWXByCategory(String.valueOf(NewsCategoryEnum.GFWX.getValue()));//官方微信
		model.add("gfwx",gfwx);
		
		List<Map> gywx =  newMediaDAO.queryWXByCategory(String.valueOf(NewsCategoryEnum.GYWX.getValue()));//公益微信
		model.add("gywx",gywx);
		
		List<Map> sywx =  newMediaDAO.queryWXByCategory(String.valueOf(NewsCategoryEnum.SYWX.getValue()));//商业微信
		model.add("sywx",sywx);
		
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));// 集团品牌活动列表
		return "newmedia/wxh.vm";
	}
	
	//请求list
	@Get("lis")
	public String golist(@Param("matrixType") String matrixType,
			@Param("start") int start,@Param("length") int length)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> repls = newMediaDAO.queryWBByMatrixType(matrixType, start*length, length);
		int totalCount = newMediaDAO.queryWBByMatrixTypeCount(matrixType);
		List<Map> repsList = new ArrayList<Map>();
		for(Map rep :repls)
		{
			Map repMap = new HashMap();
			repMap.put("id", rep.get("id"));//ID
			repMap.put("title", rep.get("title"));//标题
			repMap.put("matrixType", rep.get("matrixType"));//类别
			repMap.put("category", rep.get("category"));//类别
			repMap.put("carouselImage", rep.get("carouselImage")+".thumb.jpg");//缩率图
			repMap.put("website", rep.get("website"));//网站
			repMap.put("qrCode", rep.get("qrCode"));//二维码
			repMap.put("content", rep.get("content"));//内容
			repsList.add(repMap);
		}
		
		Page p= new Page<Map>(0, length, repsList, totalCount);
		
		jo.put("list", repsList);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		return "@"+jo.toString();
	}
	
	//微信汇请求list
	@Get("lisbid")
	public String golistById(@Param("id") String id)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> repls = newMediaDAO.queryById(id);
		List<Map> repsList = new ArrayList<Map>();
		for(Map rep :repls)
		{
			Map repMap = new HashMap();
			repMap.put("id", rep.get("id"));//ID
			repMap.put("title", rep.get("title"));//标题
			repMap.put("matrixType", rep.get("matrixType"));//类别
			repMap.put("category", rep.get("category"));//类别
			repMap.put("carouselImage", rep.get("carouselImage")+".thumb.jpg");//缩率图
			repMap.put("website", rep.get("website"));//网站
			repMap.put("qrCode", rep.get("qrCode"));//二维码
			repMap.put("content", rep.get("content"));//内容
			repsList.add(repMap);
		}
		
		jo.put("list", repsList);
		return "@"+jo.toString();
	}
	
	//微信汇请求list
	@Get("lisbc")
	public String golistByCategory(@Param("category") String category,
			@Param("start") int start,@Param("length") int length)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> repls = newMediaDAO.queryWXByCategory(category, start*length, length);
		int totalCount = newMediaDAO.queryWXByCategoryCount(category);
		List<Map> repsList = new ArrayList<Map>();
		for(Map rep :repls)
		{
			Map repMap = new HashMap();
			repMap.put("id", rep.get("id"));//ID
			repMap.put("title", rep.get("title"));//标题
			repMap.put("matrixType", rep.get("matrixType"));//类别
			repMap.put("category", rep.get("category"));//类别
			repMap.put("carouselImage", rep.get("carouselImage")+".thumb.jpg");//缩率图
			repMap.put("website", rep.get("website"));//网站
			repMap.put("qrCode", rep.get("qrCode"));//二维码
			repMap.put("content", rep.get("content"));//内容
			repsList.add(repMap);
		}
		Page p= new Page<Map>(0, length, repsList, totalCount);
		
		jo.put("list", repsList);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		return "@"+jo.toString();
	}

	/**
	 * 集团down
	 * 
	 * @param category
	 * @return
	 */
	private List<Map> jtpphd(String category) {
		// 集团品牌活动
		List<Map> ppls = lineDAO.query(category, 0, 5);
		List<Map> pphdls = new ArrayList<Map>();
		for (Map rep : ppls) {
			Map l_rep = new HashMap();
			l_rep.put("id", rep.get("id"));// ID
			l_rep.put("title", rep.get("title"));// 标题
			l_rep.put("abstractInfor", rep.get("abstractInfor"));// 简介
			l_rep.put("carouselImage", rep.get("carouselImage") + ".thumb.jpg");// 缩率图
			pphdls.add(l_rep);
		}
		return pphdls;
	}
}
